package com.ml.carManager.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.ml.carManager.R;
import com.ml.carManager.ui.dialog.listener.OnCancelClickListener;
import com.ml.carManager.ui.dialog.listener.OnEnterClickListener;


public class SimpleHintDialog extends Dialog implements View.OnClickListener {

    private Context context;
    private RelativeLayout rlBG;
    private TextView enter, cancel;
    private View lineView;
    private TextView content, title;
    private String titleString = "";
    private String contentString = "您确定执行吗？";
    private String enterString = "确定";
    private int enterColor = 0;
    private String cancleString = "取消";
    /**
     * 是否显示取消按钮
     */
    private boolean isShowCancelBtn = true;

    private boolean isShow = false;

    public SimpleHintDialog(Context context) {
        super(context, R.style.normal_dialog);
        this.context = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);//状态栏透明
        setContentView(R.layout.dialog_simple_hint);
        initWidget();
        init();
    }

    private void initWidget() {
        enter = (TextView) findViewById(R.id.dialog_simple_hint_enter);
        cancel = (TextView) findViewById(R.id.dialog_simple_hint_cancel);
        content = (TextView) findViewById(R.id.dialog_simple_hint_content);
        title = (TextView) findViewById(R.id.dialog_simple_hint_tittle);
        rlBG = (RelativeLayout) findViewById(R.id.dialog_simple_hint_bg);
        lineView = findViewById(R.id.dialog_simple_hint_line);

        cancel.setOnClickListener(this);
        enter.setOnClickListener(this);
    }


    private void init() {

    }


    /**
     * 设置提示语的文本
     *
     * @param contentString
     */
    public SimpleHintDialog setContentString(String contentString) {
        this.contentString = contentString;

        return this;
    }

    /**
     * 设置确定按钮的文本
     */
    public SimpleHintDialog setConfirmBtnText(String str) {
        this.enterString = str;
        return this;
    }

    /**
     * 设置确定按钮的颜色
     */
    public SimpleHintDialog setEnterTxtColor(int color) {
        this.enterColor = color;
        return this;
    }

    /**
     * 设置取消Btn显示/隐藏
     *
     * @param isShowCancelBtn
     */
    public void setShowCancel(boolean isShowCancelBtn) {
        this.isShowCancelBtn = isShowCancelBtn;
    }

    /**
     * 设置取消按钮的文本
     */
    public SimpleHintDialog setCancleBtnTxt(String str) {
        this.cancleString = str;
        return this;
    }

    /**
     * 设置标题的文本如果文本为空的话就会隐藏标题
     */
    public void setTitleString(String titleString) {
        this.titleString = titleString;
    }

    @Override
    public void show() {
        if (!isShow) {
            try {
                super.show();
            } catch (Exception e) {

            }
            content.setText(contentString);
            enter.setText(enterString);
            if (enterColor != 0)
                enter.setTextColor(ContextCompat.getColor(context, enterColor));
            if (!"".equals(titleString)) {
                title.setText(titleString);
                title.setVisibility(View.VISIBLE);
            } else {
                title.setVisibility(View.GONE);
            }
            if (!isShowCancelBtn) {
                cancel.setVisibility(View.GONE);
                lineView.setVisibility(View.GONE);
            } else {
                cancel.setText(cancleString);
                cancel.setVisibility(View.VISIBLE);
                lineView.setVisibility(View.VISIBLE);
            }
            isShow = true;
        }
    }

    private OnEnterClickListener onEnterClickListener = null;
    private OnCancelClickListener onCancelClickListener = null;

    public SimpleHintDialog setOnEnterClickListener(
            OnEnterClickListener onEnterClickListener) {
        this.onEnterClickListener = onEnterClickListener;

        return this;
    }

    public SimpleHintDialog setOnCancelClickListener(
            OnCancelClickListener onCancelClickListener) {
        this.onCancelClickListener = onCancelClickListener;

        return this;
    }

    @Override
    public void onClick(View v) {
        if (v == enter) {
            dismiss();
            if (onEnterClickListener != null) {
                onEnterClickListener.onEnter();
            }
        } else if (v == cancel) {
            dismiss();
            if (onCancelClickListener != null) {
                onCancelClickListener.onCancel();
            }
        }
    }
}
